//
//  UIButton+KYRoundedAndShadowedButton.m
//  Project
//
//  Created by Kai Yu on 12/18/13.
//  Copyright (c) 2013 Zhang Kai Yu. All rights reserved.
//

#import "UIButton+KYRoundedAndShadowedButton.h"



#define CORNER_RADIUS 5

@implementation UIButton (KYRoundedAndShadowedButton)
+ (UIButton *)kyRoundedAndShadowedButtonWithFrame:(CGRect)frame color:(UIColor *)color title:(NSString *)title
{
    UIImage *image = [self renderButtonImageWithSize:frame.size color:color];
    UIButton *button = nil;
    if (kIsIOS7) {
        button = [[UIButton alloc] initWithFrame:frame];
        [button setBackgroundImage:image forState:UIControlStateNormal];
        button.titleLabel.text = @"sdfsaf";
        button.titleLabel.textColor = [UIColor blackColor];
    } else {
        button = [UIButton buttonWithType:UIButtonTypeSystem];
//        button.tintColor = [UIColor colorWithRed:(159/255.0f) green:(159/255.0f) blue:(160/255.0f) alpha:1];
        button.tintColor = [UIColor whiteColor];

        button.frame = frame;
        
        
    //    UIImage *buttonBackgroundImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
       
        
     //   NSString *title = title;
        
        NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:title attributes:@{NSFontAttributeName:[UIFont fontWithName:@"FZLTXHK" size:15]}];
        [button setAttributedTitle:attributedString forState:UIControlStateNormal];
//        [button setBackgroundImage:buttonBackgroundImage forState:UIControlStateNormal];

    }

    button.layer.shadowRadius = 0.5;
    button.titleLabel.font=[UIFont boldSystemFontOfSize:20.0f];
    button.layer.shadowColor = [UIColor grayColor].CGColor;
    button.layer.shadowOffset = CGSizeMake(1, 1);
    button.layer.shadowOpacity = 0.5;
    
    return button;
}
+ (UIButton *)kyRoundedAndShadowedButtonWithFrame:(CGRect)frame color:(UIColor *)color title:(NSString *)title sizefont:(CGFloat)size;
{
    UIImage *image = [self renderButtonImageWithSize:frame.size color:color];
    UIButton *button = nil;
    if (kIsIOS7) {
        button = [[UIButton alloc] initWithFrame:frame];
        [button setBackgroundImage:image forState:UIControlStateNormal];
        button.titleLabel.text = @"sdfsaf";
        button.titleLabel.textColor = [UIColor blackColor];
    } else {
        button = [UIButton buttonWithType:UIButtonTypeSystem];
        //        button.tintColor = [UIColor colorWithRed:(159/255.0f) green:(159/255.0f) blue:(160/255.0f) alpha:1];
        button.tintColor = [UIColor whiteColor];

        button.frame = frame; 
        
        
        UIImage *buttonBackgroundImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        
        //   NSString *title = title;
        
        NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:title attributes:@{NSFontAttributeName:[UIFont fontWithName:@"FZLTXHK" size:size]}];
        [button setAttributedTitle:attributedString forState:UIControlStateNormal];
        //        [button setBackgroundImage:buttonBackgroundImage forState:UIControlStateNormal];
        
    }
    
    button.layer.shadowRadius = 0.5;
    button.layer.cornerRadius=4;
    button.titleLabel.font=[UIFont boldSystemFontOfSize:20.0f];
    button.layer.shadowColor = [UIColor grayColor].CGColor;
    button.layer.shadowOffset = CGSizeMake(1, 1);
    button.layer.shadowOpacity = 0.5;
    
    return button;
}

+ (UIImage *)renderButtonImageWithSize:(CGSize)size color:(UIColor *)color
{
    CGSize adjustedSize = CGSizeMake(size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(adjustedSize, NO, [[UIScreen mainScreen] scale]);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect rect = CGRectMake(0, 0, adjustedSize.width, adjustedSize.height);
    CGPathRef path = CGPathCreateWithRoundedRect(rect, CORNER_RADIUS, CORNER_RADIUS, &CGAffineTransformIdentity);
    //CGMutablePathRef path = createRoundedRectForRect(rect, CORNER_RADIUS);
    CGContextAddPath(context, path);
    CGPathRelease(path);
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillPath(context);
    
    return UIGraphicsGetImageFromCurrentImageContext();
}



@end

